package org.apache.ibatis.common.io;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.Properties;

/**
 * @Name Resources
 * @Description: 类加载器包装类 [封装] 类
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-04-28 17:48:54
 **/
public class Resources {
    private static ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();
    private static Charset charset;

    Resources() {
    }

    public static ClassLoader getDefaultClassLoader() {
        return classLoaderWrapper.defaultClassLoader;
    }

    public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
        classLoaderWrapper.defaultClassLoader = defaultClassLoader;
    }

    public static URL getResourceURL(String resource) throws IOException {
        return getResourceURL(null, resource);
    }

    public static URL getResourceURL(ClassLoader classLoader, String resource) throws IOException {
        URL url = classLoaderWrapper.getResourceAsURL(resource, classLoader);
        if (url == null) throw new IOException("没有发现资源：" + resource);
        return url;
    }

    public static InputStream getResourceAsInputStream(String resource) throws IOException {
        return getResourceAsInputStream(null, resource);
    }

    public static InputStream getResourceAsInputStream(ClassLoader classLoader, String resource) throws IOException {
        InputStream in = classLoaderWrapper.getResourceAsInputStream(resource, classLoader);
        if (in == null) throw new IOException("没有发现资源：" + resource);
        return in;
    }

    public static Properties getResourceAsProperties(String resource) throws IOException {
        Properties properties = new Properties();
        try (InputStream in = getResourceAsInputStream(resource)) {
            properties.load(in);
        }
        return properties;
    }

    public static Properties getResourceAsProperties(ClassLoader classLoader, String resource) throws IOException {
        Properties properties = new Properties();
        try (InputStream in = getResourceAsInputStream(classLoader, resource)) {
            properties.load(in);
        }
        return properties;
    }

    public static Reader getResourceAsReader(String resource) throws IOException {
        Reader reader;
        if (charset == null) reader = new InputStreamReader(getResourceAsInputStream(resource));
        else reader = new InputStreamReader(getResourceAsInputStream(resource), charset);
        return reader;
    }

    public static Reader getResourceAsReader(ClassLoader classLoader, String resource) throws IOException {
        Reader reader;
        if (charset == null) reader = new InputStreamReader(getResourceAsInputStream(classLoader, resource));
        else reader = new InputStreamReader(getResourceAsInputStream(classLoader, resource), charset);
        return reader;
    }

    public static File getResourceAsFile(String resource) throws IOException {
        return new File(getResourceURL(resource).getFile());
    }

    public static File getResourceAsFile(ClassLoader classLoader, String resource) throws IOException {
        return new File(getResourceURL(classLoader, resource).getFile());
    }

    public static InputStream getUrlAsStream(String urlString) throws IOException {
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();
        return connection.getInputStream();
    }

    public static Reader getUrlAsReader(String urlString) throws IOException {
        Reader reader;
        if (charset == null) reader = new InputStreamReader(getUrlAsStream(urlString));
        else reader = new InputStreamReader(getUrlAsStream(urlString), charset);
        return reader;
    }

    public static Properties getUrlAsProperties(String urlString) throws IOException {
        Properties properties = new Properties();
        try (InputStream in = getUrlAsStream(urlString)) {
            properties.load(in);
        }
        return properties;
    }

    public static Class<?> classForName(String className) throws ClassNotFoundException {
        return classLoaderWrapper.classForName(className);
    }

    public static Charset getCharset() {
        return charset;
    }

    public static void setCharset(Charset charset) {
        Resources.charset = charset;
    }

}

